package com.xyht.sca_s.student_manage_system.modules.card.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ExceptionCast;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.card.entity.SmsCard;
import com.xyht.sca_s.student_manage_system.modules.card.entity.SmsCardProcess;
import com.xyht.sca_s.student_manage_system.modules.card.entity.SmsCardProcessLevel;
import com.xyht.sca_s.student_manage_system.modules.card.entity.req.SmsCardProcessLevelReq;
import com.xyht.sca_s.student_manage_system.modules.card.entity.req.SmsCardProcessReq;
import com.xyht.sca_s.student_manage_system.modules.card.entity.resp.SmsCardProcessLevelResp;
import com.xyht.sca_s.student_manage_system.modules.card.entity.resp.SmsCardProcessResp;
import com.xyht.sca_s.student_manage_system.modules.card.mapper.SmsCardMapper;
import com.xyht.sca_s.student_manage_system.modules.card.mapper.SmsCardProcessLevelMapper;
import com.xyht.sca_s.student_manage_system.modules.card.mapper.SmsCardProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.card.service.SmsCardProcessService;
import com.xyht.sca_s.student_manage_system.modules.leave.entity.resp.SmsLeaveProcessResp;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.leave.constant.LeaveAllowFlag.ALLOW_FLAG_WAIT;

/**
 * <p>
 * 证件补办流程设置 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-01-20
 */
@Service
public class SmsCardProcessServiceImpl extends ServiceImpl<SmsCardProcessMapper, SmsCardProcess> implements SmsCardProcessService {
    @Autowired
    private SmsCardProcessMapper smsCardProcessMapper;
    @Autowired
    private SmsRoleMapper smsRoleMapper;
    @Autowired
    private SmsCardMapper smsCardMapper;
    @Autowired
    private SmsCardProcessLevelMapper smsCardProcessLevelMapper;

    public SmsCardProcessServiceImpl() {
    }

    @Override
    @Transactional
    public ResponseResult addCardProcess(SmsCardProcessReq smsCardProcessReq) {
        if (isNullOrEmpty(smsCardProcessReq.getProcessName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<SmsCardProcessLevelReq> levelList = smsCardProcessReq.getLevelList();
        if (levelList == null || levelList.size() < 1) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        for (SmsCardProcessLevelReq cardProcessLevelReq : levelList) {
            if (isNullOrEmpty(cardProcessLevelReq.getRoleId()) || isNullOrEmpty(cardProcessLevelReq.getSort())) {
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
        }
        int processSort;

        //流程名查重
        QueryWrapper<SmsCardProcess> nameQueryWrapper = new QueryWrapper<>();
        nameQueryWrapper.lambda().eq(SmsCardProcess::getProcessName, smsCardProcessReq.getProcessName());
        List<SmsCardProcess> nameList = smsCardProcessMapper.selectList(nameQueryWrapper);
        if (nameList != null && nameList.size() > 0) {
            return CommonResult.failed(CommonCodeEnum.LEAVE_PROCESS_NAME_EXIST);
        }

        QueryWrapper<SmsCardProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(SmsCardProcess::getProcessSort);
        List<SmsCardProcess> list = smsCardProcessMapper.selectList(queryWrapper);
        if (list != null && list.size() > 0) {
            processSort = list.size() + 1;
        } else {
            processSort = 1;
        }

        //补办流程添加
        SmsCardProcess smsCardProcess = new SmsCardProcess();
        BeanUtils.copyProperties(smsCardProcessReq, smsCardProcess);
        smsCardProcess.setProcessSort(processSort);
        smsCardProcessMapper.insert(smsCardProcess);

        String process_id = smsCardProcess.getId();
        if (isNullOrEmpty(process_id)) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
        }

        //补办流程分级表添加
        for (SmsCardProcessLevelReq smsCardProcessLevelReq : levelList) {
            SmsRole smsRole = smsRoleMapper.selectById(smsCardProcessLevelReq.getRoleId());
            if (smsRole == null) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ROLE_NOT_EXIST));
            }
            SmsCardProcessLevel smsCardProcessLevel = new SmsCardProcessLevel();
            BeanUtils.copyProperties(smsCardProcessLevelReq, smsCardProcessLevel);
            smsCardProcessLevel.setProcessId(process_id);
            smsCardProcessLevelMapper.insert(smsCardProcessLevel);
        }
        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult deleteCardProcess(SmsCardProcess smsCardProcess) {
        if (isNullOrEmpty(smsCardProcess.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsCardProcess process = smsCardProcessMapper.selectById(smsCardProcess.getId());
        if (process == null) {
            return CommonResult.failed(CommonCodeEnum.LEAVE_PROCESS_NOT_EXIST);
        }

        //判断流程是否使用中
        QueryWrapper<SmsCard> smsCardQueryWrapper = new QueryWrapper<>();
        smsCardQueryWrapper.lambda().eq(SmsCard::getProcessId, process.getId())
                .eq(SmsCard::getIsAllow, ALLOW_FLAG_WAIT);
        List<SmsCard> leaveList = smsCardMapper.selectList(smsCardQueryWrapper);
        if (leaveList != null && leaveList.size() > 0) {
            return CommonResult.failed(CommonCodeEnum.LEAVE_PROCESS_OCCUPIED);
        }

        //删除流程
        smsCardProcessMapper.deleteById(process.getId());
        QueryWrapper<SmsCardProcessLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SmsCardProcessLevel::getProcessId, process.getId());
        smsCardProcessLevelMapper.delete(queryWrapper);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getCardProcess(Integer pageSize, Integer pageNum) {
        QueryWrapper<SmsCardProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(SmsCardProcess::getProcessSort);

        Page<SmsCardProcess> page = new Page<>(pageNum, pageSize);
        smsCardProcessMapper.selectPage(page, queryWrapper);
        List<SmsCardProcess> list = page.getRecords();
        int total = (int) page.getTotal();
        List<SmsLeaveProcessResp> respList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (SmsCardProcess smsCardProcess : list) {
                SmsLeaveProcessResp smsLeaveProcessResp = new SmsLeaveProcessResp();
                BeanUtils.copyProperties(smsCardProcess, smsLeaveProcessResp);
                respList.add(smsLeaveProcessResp);
            }
        }
        return CommonResult.success(respList,total);
    }

    @Override
    public ResponseResult getCardProcessDetail(String process_id) {
        if (isNullOrEmpty(process_id)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsCardProcess smsCardProcess = smsCardProcessMapper.selectById(process_id);
        if (smsCardProcess == null) {
            return CommonResult.failed(CommonCodeEnum.LEAVE_PROCESS_NOT_EXIST);
        }

        QueryWrapper<SmsCardProcessLevel> levelQueryWrapper = new QueryWrapper<>();
        levelQueryWrapper.lambda().eq(SmsCardProcessLevel::getProcessId, process_id)
                .orderByAsc(SmsCardProcessLevel::getSort);
        List<SmsCardProcessLevel> list = smsCardProcessLevelMapper.selectList(levelQueryWrapper);
        List<SmsCardProcessLevelResp> levelRespList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (SmsCardProcessLevel level : list) {
                SmsCardProcessLevelResp levelResp = new SmsCardProcessLevelResp();
                BeanUtils.copyProperties(level, levelResp);
                levelRespList.add(levelResp);
            }
        }
        SmsCardProcessResp processResp = new SmsCardProcessResp();
        BeanUtils.copyProperties(smsCardProcess, processResp);
        processResp.setLevelList(levelRespList);
        return CommonResult.success();
    }


}
